package com.syt.cellphone.information.utils

import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import com.syt.cellphone.information.App

/**
 *  @author: syt
 *  创建时间: 2020/8/14 15:18
 *  @功能   视图拓展
 *  @version v1.0
 */
// 禁止窗口自动填充
fun View.disableAutoFill() = run {
    val imm = App.INSTANCE.getSystemService(Context.INPUT_METHOD_SERVICE)
            as? InputMethodManager
    imm?.let {
        imm.hideSoftInputFromWindow(this.windowToken, 0)
    }
}

// 隐藏软键盘
fun View.hideSoftInput() = run {
    val imm = App.INSTANCE.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
    imm?.let {
        imm.hideSoftInputFromWindow(this.windowToken, 0)
    }
}

// tailrec = 尾递归 获得当前的context
private tailrec fun getCompatActivity(context: Context?): AppCompatActivity? {
    return when(context) {
        is AppCompatActivity -> context
        is androidx.appcompat.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
        is android.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
        else -> null
    }
}

val View.activity: AppCompatActivity?
    get() = getCompatActivity(context)